package org.example.main.controller;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.example.main.utils.CommonResult;
import org.example.main.entity.House;
import org.example.main.service.HouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@Tag(name = "房源管理", description = "房源管理操作接口")
@RestController
@RequestMapping("/house")
public class HouseController {

    @Autowired
    private HouseService houseService;


    @Operation(summary = "房东提交申请")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "申请发送成功"),
            @ApiResponse(responseCode = "500", description = "申请发送失败")
    })
    @PostMapping("/add")//http://localhost:8089/main/house/add
    public CommonResult add(@RequestBody House house) {
        house.setStatus("pending"); // 初始状态为审核中
        CommonResult commonResult = new CommonResult();
        try{
            houseService.add(house);
            commonResult.setCode(200);
            commonResult.setMsg("申请发送成功");
        }catch (Exception e){
            commonResult.setCode(500);
            commonResult.setMsg("申请发送失败");
        }
        return commonResult;
    }


    @Operation(summary = "房东修改房源")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "修改成功"),
            @ApiResponse(responseCode = "500", description = "修改失败")
    })
    @PutMapping("/update")//http://localhost:8089/main/house/update
    public CommonResult update(@RequestBody House house) {
        CommonResult commonResult = new CommonResult();
        try {
            houseService.update(house);
            commonResult.setCode(200);
            commonResult.setMsg("修改成功");
        }
        catch (Exception e){
            commonResult.setCode(500);
            commonResult.setMsg("修改失败");
        }
        return commonResult;
    }

    @Operation(summary = "删除房源")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "删除成功"),
            @ApiResponse(responseCode = "500", description = "删除失败")
    })
    @DeleteMapping("/delete/{houseId}")//http://localhost:8089/main/house/delete/{houseId}
    public CommonResult delete(@PathVariable long houseId) {
        CommonResult commonResult = new CommonResult();
        try {
            houseService.deleteById(houseId);
            commonResult.setCode(200);
            commonResult.setMsg("删除成功");
        }
        catch (Exception e){
            commonResult.setCode(500);
            commonResult.setMsg("删除失败");
        }
        return commonResult;
    }

    @Operation(summary = "查询所有房源")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "404", description = "未查询到"),
            @ApiResponse(responseCode = "500", description = "查询失败")
    })
    @GetMapping("/findAll")//http://localhost:8089/main/house/findAll
    public CommonResult findAll() {
        CommonResult commonResult = new CommonResult();
        try{
            List<House> list = houseService.findAll();
            if(!list.isEmpty()){
                commonResult.setCode(200);
                commonResult.setMsg("查询成功");
                commonResult.setData(list);
            }else{
                commonResult.setCode(404);
                commonResult.setMsg("未查询到");
            }
        }catch (Exception e){
            commonResult.setCode(500);
            commonResult.setMsg("查询失败");
        }
        return commonResult;
    }


    @Operation(summary = "根据房东ID查询房源")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "500", description = "查询失败")
    })
    @GetMapping("/landlord/{landlordId}")//http://localhost:8089/main/house/landlord/{landlordId}
    public CommonResult getByLandlord(@PathVariable long landlordId) {
        CommonResult commonResult = new CommonResult();
        try{
            List<House> list = houseService.findByLandlordId(landlordId);
            commonResult.setCode(200);
            commonResult.setMsg("查询成功");
            commonResult.setData(list);
        }catch (Exception e){
            commonResult.setCode(500);
            commonResult.setMsg("查询失败");
        }
        return commonResult;
    }

    @Operation(summary = "根据状态查询房源")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "500", description = "查询失败")
    })
    @GetMapping("/status")//http://localhost:8089/main/house/status
    public CommonResult getByStatus(@RequestParam String status) {
        CommonResult commonResult = new CommonResult();
        try{
            List<House> list = houseService.findByStatus(status);
            commonResult.setCode(200);
            commonResult.setMsg("查询成功");
            commonResult.setData(list);
        }catch (Exception e){
            commonResult.setCode(500);
            commonResult.setMsg("查询失败");
        }
        return commonResult;
    }


    @Operation(summary = "根据房屋ID查找房源")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "500", description = "查询失败")
    })
    @GetMapping("/{houseId}")//http://localhost:8089/main/house/{houseId}
    public CommonResult getById(@PathVariable long houseId) {
        CommonResult commonResult = new CommonResult();
        try{
            House house = houseService.findById(houseId);
            commonResult.setCode(200);
            commonResult.setMsg("查询成功");
            commonResult.setData(house);
        }catch (Exception e){
            commonResult.setCode(500);
            commonResult.setMsg("查询失败");
        }
        return commonResult;
    }

    @Operation(summary = "更新房源状态")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "更新成功"),
            @ApiResponse(responseCode = "500", description = "更新失败")
    })
    @PutMapping("/statu")//http://localhost:8089/main/house/statu
    public CommonResult updateStatus(@RequestParam long houseId, @RequestParam String status) {
        CommonResult commonResult = new CommonResult();
        try {
            houseService.updateStatus(houseId, status);
            commonResult.setCode(200);
            commonResult.setMsg("更新成功");
        }
        catch (Exception e){
            commonResult.setCode(500);
            commonResult.setMsg("更新失败");
        }
        return commonResult;
    }

    @Operation(summary = "根据名称查询房源")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "404", description = "未查询到"),
            @ApiResponse(responseCode = "500", description = "查询失败")
    })
    @GetMapping("/title/{title}")//http://localhost:8089/main/house/title/{title}
    public CommonResult findByTitle(@PathVariable String title) {
        CommonResult commonResult = new CommonResult();
        try{
            List<House> list = houseService.findByTitle(title);
            if(!list.isEmpty()){
                commonResult.setCode(200);
                commonResult.setMsg("查询成功");
                commonResult.setData(list);
            }else{
                commonResult.setCode(404);
                commonResult.setMsg("未查询到");
            }
        }catch (Exception e){
            commonResult.setCode(500);
            commonResult.setMsg("查询失败");
        }
        return commonResult;
    }

    @Operation(summary = "根据地区查询房源")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "404", description = "未查询到"),
            @ApiResponse(responseCode = "500", description = "查询失败")
    })
    @GetMapping("/area/{area}")//http://localhost:8089/main/house/area/{area}
    public CommonResult findByArea(@PathVariable String area) {
        CommonResult commonResult = new CommonResult();
        try{
            List<House> list = houseService.findByArea(area);
            if(!list.isEmpty()){
                commonResult.setCode(200);
                commonResult.setMsg("查询成功");
                commonResult.setData(list);
            }else{
                commonResult.setCode(404);
                commonResult.setMsg("未查询到");
            }
        }catch (Exception e){
            commonResult.setCode(500);
            commonResult.setMsg("查询失败");
        }
        return commonResult;
    }

}
